import request from '@/utils/request'

// 定义通用API响应类型
interface ApiResponse<T> {
  code: number
  msg: string
  data: T
  count?: number
}

// 报销
interface Expense {
  id: number
  admin_id: number
  cate_id: number
  amount: number
  expense_time: string
  remark: string
  check_status: number
  pay_status: number
  create_time: string
  update_time: string
}

// 发票
interface Invoice {
  id: number
  admin_id: number
  cate_id: number
  amount: number
  invoice_time: string
  check_status: number
  create_time: string
  update_time: string
}

// 票据
interface Ticket {
  id: number
  admin_id: number
  cate_id: number
  amount: number
  ticket_time: string
  check_status: number
  create_time: string
  update_time: string
}

// 获取报销列表
export function getExpenseList(params: any): Promise<ApiResponse<Expense[]>> {
  return request({
    url: '/api/finance/expense/index',
    method: 'get',
    params,
  })
}

// 获取报销详情
export function getExpenseDetail(id: number): Promise<ApiResponse<Expense>> {
  return request({
    url: '/api/finance/expense/detail',
    method: 'get',
    params: { id },
  })
}

// 新增报销
export function addExpense(data: any) {
  return request({
    url: '/api/finance/expense/add',
    method: 'post',
    data,
  })
}

// 编辑报销
export function editExpense(data: any) {
  return request({
    url: '/api/finance/expense/edit',
    method: 'post',
    data,
  })
}

// 删除报销
export function deleteExpense(id: number) {
  return request({
    url: '/api/finance/expense/delete',
    method: 'post',
    data: { id },
  })
}

// 审批报销
export function checkExpense(data: { id: number; check_status: number; remark: string }) {
  return request({
    url: '/api/finance/expense/check',
    method: 'post',
    data,
  })
}

// 打款报销
export function payExpense(data: { id: number; pay_status: number }) {
  return request({
    url: '/api/finance/expense/pay',
    method: 'post',
    data,
  })
}

// 获取发票列表
export function getInvoiceList(params: any): Promise<ApiResponse<Invoice[]>> {
  return request({
    url: '/api/finance/invoice/index',
    method: 'get',
    params,
  })
}

// 获取发票详情
export function getInvoiceDetail(id: number): Promise<ApiResponse<Invoice>> {
  return request({
    url: '/api/finance/invoice/detail',
    method: 'get',
    params: { id },
  })
}

// 新增发票
export function addInvoice(data: any) {
  return request({
    url: '/api/finance/invoice/add',
    method: 'post',
    data,
  })
}

// 编辑发票
export function editInvoice(data: any) {
  return request({
    url: '/api/finance/invoice/edit',
    method: 'post',
    data,
  })
}

// 删除发票
export function deleteInvoice(id: number) {
  return request({
    url: '/api/finance/invoice/delete',
    method: 'post',
    data: { id },
  })
}

// 开具发票
export function openInvoice(data: { id: number }) {
  return request({
    url: '/api/finance/invoice/open',
    method: 'post',
    data,
  })
}

// 作废发票
export function voidInvoice(data: { id: number }) {
  return request({
    url: '/api/finance/invoice/void',
    method: 'post',
    data,
  })
}

// 恢复发票
export function unvoidInvoice(data: { id: number }) {
  return request({
    url: '/api/finance/invoice/unvoid',
    method: 'post',
    data,
  })
}

// 获取票据列表
export function getTicketList(params: any): Promise<ApiResponse<Ticket[]>> {
  return request({
    url: '/api/finance/ticket/index',
    method: 'get',
    params,
  })
}

// 获取票据详情
export function getTicketDetail(id: number): Promise<ApiResponse<Ticket>> {
  return request({
    url: '/api/finance/ticket/detail',
    method: 'get',
    params: { id },
  })
}

// 新增票据
export function addTicket(data: any) {
  return request({
    url: '/api/finance/ticket/add',
    method: 'post',
    data,
  })
}

// 编辑票据
export function editTicket(data: any) {
  return request({
    url: '/api/finance/ticket/edit',
    method: 'post',
    data,
  })
}

// 删除票据
export function deleteTicket(id: number) {
  return request({
    url: '/api/finance/ticket/delete',
    method: 'post',
    data: { id },
  })
}

// 作废票据
export function voidTicket(data: { id: number }) {
  return request({
    url: '/api/finance/ticket/void',
    method: 'post',
    data,
  })
}

// 恢复票据
export function unvoidTicket(data: { id: number }) {
  return request({
    url: '/api/finance/ticket/unvoid',
    method: 'post',
    data,
  })
}

// 获取报销类型列表
export function getExpenseCategories(params: any): Promise<ApiResponse<any[]>> {
  return request({
    url: '/api/finance/expense_cate/index',
    method: 'get',
    params,
  })
}

// 获取发票类型列表
export function getInvoiceCategories(params: any): Promise<ApiResponse<any[]>> {
  return request({
    url: '/api/finance/invoice_cate/index',
    method: 'get',
    params,
  })
}

// 获取票据类型列表
export function getTicketCategories(params: any): Promise<ApiResponse<any[]>> {
  return request({
    url: '/api/finance/ticket_cate/index',
    method: 'get',
    params,
  })
}
